<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Kategori extends CI_Controller {
	
	function __construct() {
        parent::__construct();	
		$this->isLogin();	
		$this->load->model('kategori_model');
		$this->num_post=10;
	}
	
	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}

	public function index($offset = 0)
	{
		
		$data['main_view']='kategori/main';
		$data['aksi_cari']='admin_kategori/cari_kategori';
		$uri_segment = 3;
		$num_post=10;
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		$data['hasil']=$this->kategori_model->getData($num_post, $offset);
		//echo "<pre>";
		
		foreach($data['hasil'] as $key=>$val){
			$cek_client=$this->kategori_model->cek_client($val['id_kategori']);
			if($cek_client==true){
				$status="ya";
			}else{
				$status="tidak";
			}
			$data['hasil'][$key]['status']=$status;
		}
		//print_r($data['hasil']);
		$data['offset']=$offset;
		$num_rows = $this->kategori_model->count_all_num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'admin_kategori/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('admin_template',$data);
	}

	public function cari_kategori($offset = 0){
		$data['main_view']='kategori/main';
		$data['aksi_cari']='admin_kategori/cari_kategori';
		$uri_segment = 3;
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		
		//buat cari
		if(isset($_POST['cari'])){
			$data['param_judul'] = $this->input->post('judul');
			$this->session->set_userdata('param_judul', $data['param_judul']);
		} else {
			$data['param_judul'] = $this->session->userdata('param_judul');
		}
		
		$data['hasil']=$this->kategori_model->getData($this->num_post, $offset,$data['param_judul']);

		foreach($data['hasil'] as $key=>$val){
			$cek_client=$this->kategori_model->cek_client($val['id_kategori']);
			if($cek_client==true){
				$status="ya";
			}else{
				$status="tidak";
			}
			$data['hasil'][$key]['status']=$status;
		}
		
		$data['offset']=$offset;
		$num_rows = $this->kategori_model->count_all_num_rows($data['param_judul']);
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'admin_post/cari_berita';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('admin_template',$data);
	}
	
	function view_add(){
		$data['main_view']='kategori/view_add';
		$this->load->view('admin_template',$data);
	}
	
	function proses_add(){
		$judul=$this->input->post('judul');
		$konten=$this->input->post('konten');
		
		
		if($query=$this->kategori_model->insert_data($judul)){
			$this->session->set_flashdata('pesan', 'Data Sukses Ditambah');
			redirect('admin_kategori');
			
		}
	}
	
	function view_edit($id){
		$data['main_view']='kategori/view_edit';
		$data['hasil']=$this->kategori_model->get_detail_data($id);
		$this->load->view('admin_template',$data);
	}
	
	function proses_update(){
		$judul=$this->input->post('judul');
		
		$id=$this->input->post('id');
		
		
		
		if($query=$this->kategori_model->update_data($id,$judul)){
			$this->session->set_flashdata('pesan', 'Data Sukses Ditambah');
			redirect('admin_kategori');
			
		}
	}
	
	function delete($id){
		$this->kategori_model->delete_data($id);
		$this->session->set_flashdata('pesan', 'Data Sukses Dihapus');
		redirect('admin_kategori');
	}
}